package club.dmyang.service.impl;

import club.dmyang.service.AutheService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

/**
 * Created by brss on 2018/6/29.
 */
@Service
public class AutheServiceImpl implements AutheService {
    @Override
    public boolean login(String name, String password) {
        //1.获取Subject
        Subject subject = SecurityUtils.getSubject();
        //2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        //3.执行登录方法
        try {
            //无异常就算登录成功
            subject.login(token);
            return true;
        } catch (Exception e) {
            //实际项目中是不会提示用户名是否存在，所以捕获一个大异常即可
            return false;
        }
    }

    @Override
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }
}
